import sys
# Importando algunas librerías que utilizaremos
# Networkx para grafos
import networkx as nx
# Pandas
import pandas as pd
# Mostrar imágenes
from IPython.display import HTML
# Mathplotlib
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['figure.figsize'] = (20.0, 10.0)
credenciales = pd.read_csv('credenciales.csv')
credenciales.head()
| codigo | sexo | nombre | |
|---|---|---|---|
| 0 | AMM | Masculino | Arodi Mendieta |
| 1 | BMM | Masculino | Brahian Mendieta |
| 2 | CFM | Masculino | Cris Flores |
| 3 | EBM | Masculino | Erick Barba |
| 4 | EPM | Masculino | Erubey Pérez |
credenciales.set_index(["codigo"], inplace=True)
credenciales.head()
| sexo | nombre | |
|---|---|---|
| codigo | ||
| AMM | Masculino | Arodi Mendieta |
| BMM | Masculino | Brahian Mendieta |
| CFM | Masculino | Cris Flores |
| EBM | Masculino | Erick Barba |
| EPM | Masculino | Erubey Pérez |
credenciales.loc["LMF"]
sexo Femenino nombre Lili Magdaleno Name: LMF, dtype: object
interacciones = pd.read_csv("interacciones.csv")
interacciones.head()
| origen | destino | distancia | tiempo | |
|---|---|---|---|---|
| 0 | AMM | EBM | 1 | 1 |
| 1 | AMM | EPM | 1 | 1 |
| 2 | AMM | GZM | 1 | 1 |
| 3 | AMM | GTM | 1 | 1 |
| 4 | AMM | HNF | 1 | 1 |
interacciones.describe()
| distancia | tiempo | |
|---|---|---|
| count | 485.0 | 485.0 |
| mean | 1.0 | 1.0 |
| std | 0.0 | 0.0 |
| min | 1.0 | 1.0 |
| 25% | 1.0 | 1.0 |
| 50% | 1.0 | 1.0 |
| 75% | 1.0 | 1.0 |
| max | 1.0 | 1.0 |
DG = nx.DiGraph()
for row in interacciones.iterrows():
DG.add_edge(row[1]["origen"],
row[1]["destino"],
distancia=row[1]["distancia"])
DG.nodes(data=True)
NodeDataView({'AMM': {}, 'EBM': {}, 'EPM': {}, 'GZM': {}, 'GTM': {}, 'HNF': {}, 'JVF': {}, 'KPF': {}, 'LMF': {}, 'OMM': {}, 'BMM': {}, 'EPF': {}, 'LSF': {}, 'MAF': {}, 'VTM': {}, 'UGM': {}, 'EHF': {}, 'PNF': {}, 'JMF': {}, 'CFM': {}, 'CSM': {}, 'GSF': {}, 'ZRF': {}, 'LRF': {}, 'LHF': {}, 'SOF': {}, 'ACF': {}, 'FJF': {}, 'NMF': {}, 'KSF': {}, 'PZF': {}, 'HRM': {}, 'EMF': {}, 'RFM': {}, 'RMM': {}, 'ACM': {}, 'MFF': {}})
nx.draw_circular(DG,
node_color="blue",
edge_color="gray",
font_size=24,
width=2, with_labels=True, node_size=3500,
)
list(nx.all_shortest_paths(DG, source="LMF", target="SOF", weight=None))
[['LMF', 'KPF', 'SOF'], ['LMF', 'ZRF', 'SOF']]
list(nx.all_shortest_paths(DG, source="LMF", target="HRM", weight=None))
[['LMF', 'KPF', 'HRM']]
list(nx.all_shortest_paths(DG, source="LMF", target="ACM", weight=None))
[['LMF', 'OMM', 'ACM'], ['LMF', 'VTM', 'ACM']]
list(nx.dijkstra_path(DG, source="LMF", target="ACM", weight="distancia"))
['LMF', 'OMM', 'ACM']
list(nx.dijkstra_path(DG, source="LMF", target="ACM", weight="tiempo"))
['LMF', 'OMM', 'ACM']
def show_path(path):
total_distancia = 0
for i in range(len(path)-1):
origen = path[i]
destino = path[i+1]
distancia = DG[origen][destino]["distancia"]
total_distancia = total_distancia+distancia
print(" %s -> %s\n - Distancia: %s" % (
credenciales.loc[origen]["nombre"],
credenciales.loc[destino]["nombre"],
distancia)
)
print("\n Total Distancia: %s \n" % (
total_distancia)
)
show_path(['LMF', 'KPF', 'AMM'])
Lili Magdaleno -> Kenya Piedras
- Distancia: 1
Kenya Piedras -> Arodi Mendieta
- Distancia: 1
Total Distancia: 2
def get_all_shortest_paths(DiGraph, origen, destino):
print("*** All shortest paths - Origen: %s Destino: %s" % (
origen, destino
))
for weight in [None, "distancia"]:
print("* Ordenando por: %s" % weight)
paths = list(nx.all_shortest_paths(DiGraph,
source=origen,
target=destino,
weight=weight))
for path in paths:
print(" Camino óptimo: %s" % path)
show_path(path)
def get_all_shortest_paths(DiGraph, origen, destino):
print("*** All shortest paths - Origen: %s Destino: %s" % (
origen, destino
))
for weight in [None, "distancia"]:
print("* Ordenando por: %s" % weight)
paths = list(nx.all_shortest_paths(DiGraph,
source=origen,
target=destino,
weight=weight))
for path in paths:
print(" Camino óptimo: %s" % path)
show_path(path)
get_all_shortest_paths(DG, origen="LMF", destino="AMM")
*** All shortest paths - Origen: LMF Destino: AMM
* Ordenando por: None
Camino óptimo: ['LMF', 'AMM']
Lili Magdaleno -> Arodi Mendieta
- Distancia: 1
Total Distancia: 1
* Ordenando por: distancia
Camino óptimo: ['LMF', 'AMM']
Lili Magdaleno -> Arodi Mendieta
- Distancia: 1
Total Distancia: 1
def plot_shortest_path(path):
print(path)
positions = nx.circular_layout(DG)
nx.draw(DG, pos=positions,
node_color='blue',
edge_color='gray',
font_size=24,
width=1, with_labels=True, node_size=3500, alpha=0.8
)
short_path=nx.DiGraph()
for i in range(len(path)-1):
short_path.add_edge(path[i], path[i+1])
nx.draw(short_path, pos=positions,
node_color='green',
edge_color='green',
font_size=24,
width=3, with_labels=True, node_size=3000
)
plt.show()
def get_shortest_path(DiGraph, origen, destino):
print("*** Origen: %s Destino: %s" % (origen, destino))
for weight in [None, "distancia"]:
print(" Ordenado por: %s" % weight)
path = list(nx.astar_path(DiGraph,
(origen),
(destino),
weight=weight
))
print(" Camino óptimo: %s " % path)
show_path(path)
plot_shortest_path(path)
get_shortest_path(DG, origen="LMF", destino="SOF")
*** Origen: LMF Destino: SOF
Ordenado por: None
Camino óptimo: ['LMF', 'KPF', 'SOF']
Lili Magdaleno -> Kenya Piedras
- Distancia: 1
Kenya Piedras -> Sandra Osorno
- Distancia: 1
Total Distancia: 2
['LMF', 'KPF', 'SOF']
Ordenado por: distancia
Camino óptimo: ['LMF', 'KPF', 'SOF']
Lili Magdaleno -> Kenya Piedras
- Distancia: 1
Kenya Piedras -> Sandra Osorno
- Distancia: 1
Total Distancia: 2
['LMF', 'KPF', 'SOF']
path = ['LMF', 'SOF']
plot_shortest_path(path)
['LMF', 'SOF']